/*
 * Tulalip Library, an enhanced ORMLite library.
 * Copyright (C) 2010 Richard Schilling
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gnu.tulalip.android.widget;

import java.util.Collections;
import java.util.List;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.stmt.PreparedQuery;

/**
 * An adapter that works on lists of objects. The data is cached in memory.
 * Use ORMAdapter to use less memory but have longer runtimes.
 * 
 * @author Richard Schilling (coderroadie@gmail.com)
 * @param <T> the ORMLite defined class
 * @param <ID> the type of <T> identifier field.
 */
public abstract class ORMAdapterCached<T extends AdapterObject<T>, ID> extends ORMAdapter<T, ID> {

    /**
     * The internal cache.
     */
    private List<T> data;

    public ORMAdapterCached(RuntimeExceptionDao<T, ID> dao, PreparedQuery<T> query) {
        super(dao, query);

    }

    /**
     * Returns the list of underlying data items. If doCacheData is set, then
     * the list returned is the same for each call. If doCacheData is false,
     * then a new list is created with each call and.
     * 
     * @return
     */
    @Override
    public List<T> getList() {

        if (data == null) {
            data = dao.query(query);
            Collections.sort(data);
        }

        return data;

    }

    @Override
    public int getCount() {

        return getList().size();

    }

    /**
     * Called whenever the data is known to have changed. Notifies all listeners
     * of the change in data.
     */
    @Override
    public void refresh() {
        data = null;

        // forces fresh query
        getList();
        notifyDataSetChanged();
    }

    /**
     * Adds an item to the underlying data store, and then notifies all lists
     * that the data has changed.
     * 
     * @param newItem
     */
    @Override
    public void addItem(T newItem) {
        dao.create(newItem);
        refresh();
    }

    /**
     * Deletes the item from the underlying database and refreshes the adapter.
     * 
     * @param item the item to delete.
     */
    @Override
    public void deleteItem(T item) {
        dao.delete(item);
        refresh();
    }

    /**
     * Updates the specified item in the underlying database.
     * 
     * @param item the item to update.
     */
    @Override
    public void updateItem(T item) {
        dao.update(item);
        refresh();
    }

}
